package cn.org.donald.framework.handler;

import cn.org.donald.framework.pojo.vo.HttpResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

/**
 * Controller异常捕捉
 * @author Donald
 * @since 2020-12-27 14:55:48
 */
@RestControllerAdvice
public class ControllerExceptionHandler extends ResponseEntityExceptionHandler {

    private Logger logger = LoggerFactory.getLogger(ControllerExceptionHandler.class);

    /**
     * 捕捉自定义异常
     * @param e 异常
     * @return HttpResult
     */
    @ExceptionHandler(ServiceException.class)
    public HttpResult serviceException(ServiceException e) {
        logger.warn("正在捕抓异常:{}", e.getClass() + "");
        return new HttpResult(e.getCode(), e.getInfo());
    }
}
